package cn.jdx.tools.md2html.tools;

import org.pegdown.PegDownProcessor;

import java.io.*;
import java.nio.charset.StandardCharsets;

/*
【功能】:把markdown文档转换为html格式
[1].转换后的格式是欠缺html标准结构的，只是单纯的把markdown语法替换成了对应的标签
[2].需要再次加工后才能使用

【使用】
String htmlContent = MdToolsConversionHtml().getHtml(new File(path));
 */
public class MdToolsConversionHtml {

    public static String getHtml(File mdFile)  {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(mdFile), StandardCharsets.UTF_8));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = null;
        StringBuilder mdContent = new StringBuilder();
        try{
            while (true) {
                assert br != null;
                if ((line = br.readLine()) == null) break;
                mdContent.append(line).append("\n");
            }
            PegDownProcessor pdp = new PegDownProcessor(Integer.MAX_VALUE);
            return pdp.markdownToHtml(mdContent.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
}
